﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using BBB.Valuta.Entity;

namespace BBB.Valuta
{
    public static class WorkReceipts
    {

        private static List<Receipt> LoadReceiptsByCommand(OleDbCommand command, bool isSale)
        {
            List<Receipt> receipts = new List<Receipt>();
            using (OleDbDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Receipt receipt = new Receipt
                                          {
                                              Kurs = Decimal.Parse(reader["Kurs"].ToString()),
                                              Number = reader["CNomer"].ToString(),
                                              OutDate = DateTime.Parse(reader["Date"].ToString()),
                                              OutTime = DateTime.Parse(reader["Time"].ToString()),
                                              SummaOut = isSale ? Decimal.Parse(reader["Valut"].ToString()) : Decimal.Parse(reader["Karbov"].ToString()),
                                              SummaIn = !isSale ? Decimal.Parse(reader["Valut"].ToString()) : Decimal.Parse(reader["Karbov"].ToString()),
                                              IsSale = isSale,
                                              Val =
                                                  Settings.GetInstance().Valuta.First(
                                                      v => v.Kode == reader["Kod_Val"].ToString()).Name
                                          };
                    //CultureInfo ci = new CultureInfo("en-US", false);
                    //Settings.GetInstance().Valuta[reader["Kod_Val"].ToString()];
                    //receipt.Val = reader
                    receipts.Add(receipt);
                }

                return receipts;
            }
        }
    
        public static List<Receipt> LoadReceiptsFromDB(string directory)
        {
            List<Receipt> receipts = new List<Receipt>();
            using (OleDbConnection con =
                new OleDbConnection(
                    string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=dBase III", directory)))
            {
                con.Open();
                using (OleDbCommand command = con.CreateCommand())
                {
                    command.CommandText = "select* from OVALINB.DBF";
                    receipts.AddRange(LoadReceiptsByCommand(command, false));
                    command.CommandText = "select* from OVALOUTB.DBF";
                    receipts.AddRange(LoadReceiptsByCommand(command, true));
                    return receipts;
                }
            }
        }

        public static List<Receipt> LoadReceiptsFromXML(string fileName)
        {
            XmlSerializer xml = new XmlSerializer(typeof (List<Receipt>));
            using (TextReader textReader = new StreamReader(fileName))
            {
                return (List<Receipt>) xml.Deserialize(textReader);
            }
            return null;
        }

        public static void SaveReceiptsInXML (List<Receipt> receipts, string fileName)
        {
            try
            {
                XmlSerializer xml = new XmlSerializer(receipts.GetType());
                FileInfo fi = new FileInfo(fileName);
                if (!Directory.Exists(fi.DirectoryName))
                {
                    Directory.CreateDirectory(fi.DirectoryName);
                }

                using (TextWriter textWriter = new StreamWriter(fileName))
                {
                    xml.Serialize(textWriter, receipts);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

